package com.itcast.n9_垃圾回收;

import lombok.SneakyThrows;

import java.util.LinkedList;
import java.util.List;

/**
 * @program: -XX:+UseSerialGC -Xmx10g -verbose:gc
 * @description:
 * @author: flyduck
 * @create: 2024-09-30 16:16
 **/
public class Test13_垃圾回收算法_查看STW的现象 {
    public static void main(String[] args) {
        new PrintThread().start();//循环打印时间，如果每次打印100毫秒，说明没有影响到用户线程
        new ObjectThread().start();//创建对象引起垃圾回收
    }
}
class PrintThread extends Thread{

    @SneakyThrows
    @Override
    public void run() {
        //记录开始时间

        long last = System.currentTimeMillis();
        while(true){
            long now = System.currentTimeMillis();
            System.out.println(now - last);
            last = now;
            Thread.sleep(100);
        }
    }
}

class ObjectThread extends Thread{

    @SneakyThrows
    @Override
    public void run() {
        List<byte[]> bytes = new LinkedList<>();
        while(true){
            //最多存放8g，然后删除强引用，垃圾回收时释放8g
            if(bytes.size() >= 80){
                bytes.clear();
            }
            bytes.add(new byte[1024 * 1024 * 100]);
            Thread.sleep(10);
        }
    }
}
